import { getAgentCustomerStats } from '~/services/api/tool';
import { cosThumb } from '~/utils/util';

Page({
  data: {
    defaultImg: 'https://img1.baidu.com/it/u=415449740,540746270&fm=253&fmt=auto&app=138&f=GIF?w=500&h=500',
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    goodsListLoadStatus: 0,
    pageLoading: false,
    orderList: [],
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    backRefresh: false,
    gridConfig: {
      column: 2,
      width: 100,
      height: 100,
    },
    visible: false,
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },
  goodListTime: {
    startPlaceTime: '',
    endPlaceTime: '',
    province: '',
    phone: '',
  },
  onLoad() {
    this.init();
  },

  onShow() {
    if (!this.data.backRefresh) return;
    this.onRefresh();
    this.setData({
      backRefresh: false,
    });
  },
  //搜索
  openSearch() {
    this.setData({
      visible: true,
    });
  },
  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPageScroll(e) {
    this.pullDownRefresh && this.pullDownRefresh.onPageScroll(e);
  },

  onPullDownRefresh_(e) {
    this.loadHomePage();
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },

  async loadGoodsList(fresh = false) {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    console.log('loaddd');
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1,
    });
    const { pageSize } = this.goodListPagination;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const { data } = await getAgentCustomerStats({
        // pageNo: pageIndex,
        // pageSize: pageSize,
        param: {
          endTime: this.goodListTime.endPlaceTime,
          startTime: this.goodListTime.startPlaceTime,
          province: this.goodListTime.province,
          phone: this.goodListTime.phone,
        },
      });
      let orderList = [];
      if (data && data.length) {
        orderList = data || [];
      }

      this.setData({
        orderList: fresh ? orderList : this.data.orderList.concat(orderList),
        goodsListLoadStatus: orderList.length < 10 ? 2 : 0,
      });

      // this.goodListPagination.pageSize = data.pageSize;
      // this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3,
      });
    }
  },

  init(status) {
    this.refreshList(status);
  },

  async getOrderList(statusCode, reset = false) {
    this.setData({
      listLoading: 1,
    });
    const { pageSize } = this.goodListPagination;
    const pageIndex = this.goodListPagination.pageNo + 1;
    const { data } = await getAgentCustomerStats({
      param: {
        // orderStatus: this.goodListTime.orderStatus,
        endTime: this.goodListTime.endPlaceTime,
        startTime: this.goodListTime.startPlaceTime,
        province: this.goodListTime.province,
        phone: this.goodListTime.phone,
      },
      // pageNo: 1,
      // pageSize: pageSize,
    });
    let orderList = [];
    if (data && data.length > 0) {
      orderList = data || [];
      this.setData({
        orderList,
        listLoading: data.length < 10 ? 2 : 0,
      });
      if (orderList.length < 10) {
        this.setData({
          goodsListLoadStatus: 2,
        });
      }
    } else {
      this.setData({
        orderList: [],
        listLoading: 2,
        pullDownRefreshing: false,
      });
    }
  },

  onReTryLoad() {
    this.getOrderList(this.data.curTab);
  },

  refreshList(status) {
    this.page = {
      size: 10,
      num: 1,
    };
    this.setData({
      orderList: [],
    });
    this.getOrderList(status, true);
  },

  onRefresh() {
    this.refreshList();
  },
  onHSearchHide(e) {
    const { hide } = e.detail;
    this.setData({
      visible: hide,
    });
  },
  onSearch(e) {
    const { endPlaceTime, province, startPlaceTime, phone } = e.detail;
    this.goodListTime = {
      endPlaceTime,
      orderStatus,
      province,
      phone,
      startPlaceTime,
    };
    this.setData({
      visible: false,
    });
    this.init();
  },
});
